package com.tqfframe.datamanage.controller;

import com.tqfframe.bean.CustomBean;
import com.tqfframe.beans.CustomBean2;
import com.tqfframe.beans.CustomBean3;
import com.tqfframe.common.config.fistStarter.TestDomain;
import com.tqfframe.common.exception.BaseException;
import com.tqfframe.common.jwt.JwtUtil;
import com.tqfframe.common.jwt.SysUser;
import com.tqfframe.datamanage.client.UserClient;
import com.tqfframe.datamanage.event.DemoEvent;
import com.tqfframe.datamanage.event.publish.DemoPublisher;
import com.tqfframe.testStarter.domain.Hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 唐祺峰
 * @Date: 2020/7/2 0002 13:37
 * @Version 1.1
 */
@RestController
public class stormTestController {

    Logger logger = LoggerFactory.getLogger(stormTestController.class);

    @Autowired
    private TestDomain testDomain;

    @Autowired
    private Hello hello;

    @Autowired
    private UserClient userClient;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    DemoPublisher demoPublisher;

    @Autowired
    private CustomBean customBean;
    @Autowired
    private CustomBean2 customBean2;
    @Autowired
    private CustomBean3 customBean3;
    @Autowired
    private ApplicationContext applicationContext;


    /**
     *  本地
     *  http://localhost:9009/test/11111
     *  http://localhost:8080/api/data/test/11111
     *
     *  docker
     *  http://192.168.47.135:9009/test/11111
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/test/{name}")
    public String test(@PathVariable(name = "name") String name){
        System.out.println(testDomain.getValue());
        System.out.println(testDomain.getValue2());
        System.out.println(hello.sayHello());
        logger.info(name);
        logger.debug(name);
        logger.error(name);
        try {
            throw new BaseException(11111,"xxxx",HttpStatus.ACCEPTED);
//            throw new RuntimeException();
        }catch (BaseException e){
            logger.error(e.message);
        }catch (Exception e){
            logger.error(e.toString());
        }
        return  "111";
    }

    /**
     *  本地
     *  http://localhost:9009/testTimeOut/11111
     *  http://localhost:8080/api/data/testTimeOut/11111
     *
     *  docker
     *  http://192.168.47.135:9009/testTimeOut/11111
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/testTimeOut/{name}")
    public String testTimeOut(@PathVariable(name = "name") String name){
        System.out.println(userClient.userClickTimeOut("11"));
        System.out.println("我调用了B服务");
        System.out.println(customBean.getName());
        System.out.println(customBean2.getName());
        System.out.println(customBean3.getName());
        for(String a:applicationContext.getBeanDefinitionNames()){
            System.out.println(a);
        }

//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return  "222";
    }

    /**
     *     http://localhost:9009/testlogin
     {
     "username": "zszxz",
     "password": "123456"
     }
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/testlogin",method = RequestMethod.POST)
    public Map<String, Object> testlogin(@RequestBody SysUser sysUser){
        Map<String, Object> map = new HashMap<>();
        String username = sysUser.getUsername();
        String password = sysUser.getPassword();
        // 省略 账号密码验证
        if(!password.equals("123456")){
            map.put("code", "403");
            map.put("message","认证失败");
            return map;
        }
        // 验证成功后发送token
        String token = JwtUtil.sign(sysUser,password);
        if (token != null){
            map.put("code", "200");
            map.put("message","认证成功");
            map.put("token", token);
            return map;
        }
        map.put("code", "403");
        map.put("message","认证失败");
        return map;
    }


    @RequestMapping(value = "/testPost",method = RequestMethod.POST)
    public String issueByIds(@RequestBody  Map<String,Object> m){
        String userInfo=  JwtUtil.getUserNameByToken(request);
        return userInfo;
    }

    /**
     *  http://localhost:9009/testevent
     * @return
     */
    @GetMapping(value = "/testevent")
    public String testevent(){
        ArrayList<Map> list = new ArrayList<>();
        HashMap<String, String> m1 =  new HashMap<>();
        m1.put("1", "2");
        HashMap<String, String> m2 =  new HashMap<>();
        m2.put("3", "4");
        HashMap<String, String> m3 =  new HashMap<>();
        m3.put("5", "6");
        list.add(m1);
        list.add(m2);
        list.add(m3);
        System.out.println("开始发布消息: " );
        demoPublisher.publish(new DemoEvent(this,"测试消息",list));
        System.out.println("消息发布结束: " );
        return "消息发布成功";
    }

}
